package com.gome.ocean.dao.model.reqres;

import java.util.List;
import java.util.Map;

import com.gome.ocean.common.enums.authority.UrlMethodType;
import com.gome.ocean.dao.model.authority.Function;
import com.gome.ocean.dao.model.authority.User;
import com.google.common.collect.Maps;

/**
 * Created by liuqingxia on 2016/6/2.<br>
 * 用户功能权限列表，存储于session
 */
public class UserFunctionTable {

    private User                  user;
    private Map<String, Function> functionMap;

    public UserFunctionTable(User user){
        this.user = user;
        this.functionMap = Maps.newHashMap();
    }

    public void putFunction(Function function) {
        functionMap.put(generateFuncKey(function), function);
    }

    public void putFunction(List<Function> functions) {
        for (Function function : functions) {
            putFunction(function);
        }
    }

    public void removeFunction(Function function) {
        functionMap.remove(generateFuncKey(function));
    }

    public Function getFunction(String url, String methodType) {
        return functionMap.get(generateFuncKey(url, methodType));
    }

    public boolean haveFunction(String url, String methodType) {
        return null == functionMap ? false : functionMap.containsKey(generateFuncKey(url, methodType));
    }

    private String generateFuncKey(Function function) {
        String type = UrlMethodType.getUrlMethodType(function.getMethodType()).getTypeStr();
        String key = generateFuncKey(function.getUrl(), type);
        return key;
    }

    private String generateFuncKey(String url, String methodType) {
        return url.concat("+").concat(methodType);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map<String, Function> getFunctionMap() {
        return functionMap;
    }

    public void setFunctionMap(Map<String, Function> functionMap) {
        this.functionMap = functionMap;
    }
}
